package com.niit.onlinemusicserver.common;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RandomCodeGenerate {
    /**
     * 随机生成指定长度的数字
     * @param length 制定的数字长度
     */
    public static Integer createCode(int length) throws NoSuchAlgorithmException {
        StringBuffer buffer = new StringBuffer();
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        int l = (int)Math.pow(10,length-1);
        //4 1000-9999 1000+随机（0-9000）
        //6 100000-999999 100000+随机（0-900000）
        return l+random.nextInt(9*l);
    }

    /**
     * @Description: 订单编号生成器
     * @Author: Jason
     * @date: 2023.6.29
     * @Return String
     */
    private static long orderNum = 0L;
    private static String date;
    public static synchronized String createOrderNo(){
        String str = new SimpleDateFormat("yyMMddHHmm").format(new Date());
        if(date==null||!date.equals(str)){
            date = str;
            orderNum  = 0L;
        }
        orderNum ++;
        long orderNo = Long.parseLong((date)) * 10000;
        orderNo += orderNum;;
        return orderNo+"";
    }
}
